import os
import uuid

from django.db import models

from django.contrib.auth.models import User
# Create your models here.
from django.utils.timezone import now


# 创建赛事模型

class Category(models.Model):
    name = models.CharField(max_length=100, verbose_name='竞赛名称', unique=True)

    def __str__(self):
        return self.name


def match_cover_path(instance, filename):
    """
    :param instance:
    :param filename:
    :return:
    """
    ext = filename.split('.')[-1]
    filename = '{}.{}'.format(uuid.uuid4().hex[:10], ext)
    return os.path.join("match", filename)  # 'files/38c57709f8.png'


class Match(models.Model):
    """
    赛事信息
    """
    author = models.ForeignKey('users.UserProfile', verbose_name='作者', on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    STATUS_CHOICES = (
        ('d', '草稿'),
        ('p', '发表'),
    )
    # BOOL_CHOICES = (
    #     ('true', 'true'),
    #     ('false', 'false')
    # )

    is_banner = models.BooleanField(verbose_name='是否放上轮播图',
                                    default=False)
    is_selected = models.BooleanField(verbose_name='是否精选',
                                      default=False)

    official_url = models.URLField('官方网站')
    title = models.CharField('标题', max_length=200, unique=True)
    introduction = models.TextField('简介', null=True, blank=True)
    body = models.TextField('正文')
    pub_date = models.DateTimeField('发布时间', default=now, null=True)
    create_date = models.DateTimeField('创建时间', auto_now_add=True)
    mod_date = models.DateTimeField('修改时间', auto_now=True)
    status = models.CharField('赛事状态', max_length=1, choices=STATUS_CHOICES, default='p')
    views = models.PositiveIntegerField('浏览量', default=0)
    cover = models.ImageField(upload_to=match_cover_path, verbose_name="赛事封面", null=True, blank=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "赛事信息"

    pass


class JoinInfo(models.Model):
    team = models.ForeignKey('match.Team', on_delete=models.CASCADE)  # 这个被保护就删除不了了
    user = models.ForeignKey('users.UserProfile', on_delete=models.CASCADE)
    join_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user.nickname


class Team(models.Model):
    members = models.ManyToManyField('users.UserProfile', related_name='teams', through=JoinInfo,
                                     verbose_name='队员')  # 如果多对多，通过模型是不会显示的

    match = models.ForeignKey('Match', on_delete=models.CASCADE, verbose_name='比赛项目')

    leader = models.ForeignKey('users.UserProfile', verbose_name='队长',
                                  on_delete=models.PROTECT, help_text='队长被删除队伍不会被删除, 谁创建队伍谁就是队长, 一个队长可以建立多个团妒',
                                  )

    name = models.CharField(max_length=50, verbose_name='队名')



    def __str__(self):
        return self.name




class Test(models.Model):
    name = models.CharField(max_length=10)
    team = models.ManyToManyField(Team)
